<?php

namespace App\Service;

use App\Model\Admin\AdminModel;
use App\Model\BaseModel;
use App\Traits\LogTrait;
use App\Traits\RedisTrait;
use App\Traits\ResponseTrait;
use EasySwoole\Component\Context\ContextManager;
use EasySwoole\Http\Request;
use Swoole\Exception;

/**
 * 服务基础类
 * @property BaseModel $model
 * @property AdminModel $admin
 */
class BaseService
{
    use RedisTrait;
    use ResponseTrait;
    use LogTrait;

    protected $model;
    protected $admin;

    public function __construct()
    {
        $this->admin = ContextManager::getInstance()->get('user');
    }

    /**
     * 获取数据列表
     * @return array
     * @throws Exception
     * @throws \EasySwoole\ORM\Exception\Exception
     * @throws \Throwable
     */
    public function getList()
    {
        // 初始化变量
        $where = [];
        $sorts = ['id' => 'desc'];

        // 获取参数
        $argList = func_get_args();
        if (!empty($argList)) {
            // 查询条件
            $where = (isset($argList[0]) && !empty($argList[0])) ? $argList[0] : [];
            // 排序
            $sorts = (isset($argList[1]) && !empty($argList[1])) ? $argList[1] : ['id' => 'desc'];
        }

        // 常规查询条件
        $param = $this->getRequestParam();
        if ($param) {
            // 筛选名称
            if (isset($param['name']) && $param['name']) {
                $where[] = ['name' => ["%{$param['name']}%", 'like']];
            }

            // 筛选标题
            if (isset($param['title']) && $param['title']) {
                $where[] = ['title' => ["%{$param['title']}%", 'like']];
            }

            // 筛选类型
            if (isset($param['type']) && $param['type']) {
                $where[] = ['type' => [$param['type']]];
            }

            // 筛选状态
            if (isset($param['status']) && $param['status']) {
                $where[] = ['status' => [$param['status']]];
            }

            // 手机号码
            if (isset($param['mobile']) && $param['mobile']) {
                $where[] = ['mobile' => [$param['mobile']]];
            }
        }

        return $this->model->page(PAGE, PERPAGE)->getList($where, $sorts);
    }

    /**
     * 获取记录详情
     * @return array
     * @throws Exception
     * @throws \EasySwoole\Mysqli\Exception\Exception
     * @throws \EasySwoole\ORM\Exception\Exception
     * @throws \Throwable
     */
    public function info()
    {
        // 记录ID
        $id = $this->getRequestParam('id');
        $info = [];
        if ($id) {
            $info = $this->model->getInfoById($id);
        }
        return $info;
    }

    /**
     * 添加或编辑
     * @return bool|int
     * @throws Exception
     * @throws \EasySwoole\Mysqli\Exception\Exception
     * @throws \EasySwoole\ORM\Exception\Exception
     * @throws \Throwable
     */
    public function edit()
    {
        // 获取参数
        $argList = func_get_args();
        // 查询条件
        $data = $argList[0] ?? [];
        if (!$data) {
            $data = $this->getRequestParam();
        }
        return $this->model->edit($data);
    }

    /**
     * 删除记录
     * @return bool
     * @throws Exception
     * @throws \EasySwoole\Mysqli\Exception\Exception
     * @throws \EasySwoole\ORM\Exception\Exception
     * @throws \Throwable
     */
    public function destroy()
    {
        // 参数
        $ids = $this->getRequestParam('id'); // 记录ID
        return $this->model->softDestroy($ids);
    }

    /**
     * 设置状态
     * @return bool|int
     * @throws Exception
     * @throws \EasySwoole\Mysqli\Exception\Exception
     * @throws \EasySwoole\ORM\Exception\Exception
     * @throws \Throwable
     */
    public function status()
    {
        $id = $this->getRequestParam('id'); // 记录ID

        $status = $this->getRequestParam('status'); // 记录状态

        $item = [
            'id' => $id,
            'status' => $status
        ];
        return $this->model->edit($item);
    }

    /**
     * @return array
     * @throws \EasySwoole\ORM\Exception\Exception
     * @throws \Throwable
     */
    public function getIdName()
    {
        return $this->model->getIdName();
    }

    /**
     * 返回指定的请求参数
     * @param null $param
     * @return array|mixed|object|null
     * @throws Exception
     * @author LZH
     */
    protected function getRequestParam($param = null)
    {
        /* @var Request $request */
        $request = ContextManager::getInstance()->get('request');
        if (empty($request)) {
            throw new Exception('request对象为空');
        }
        if (empty($param)) {
            return $request->getRequestParam();
        }
        return $request->getRequestParam($param);
    }
}
